<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var countSegments = function(s) {
				let count = 0;
				// 加上前导空格是为了简化后面的判断条件
				s = " " + s;
				for (let i = 0; i < s.length; i++) {
					if (s[i] != " " && s[i - 1] == " ") count++;
				}
				return count;
			};
			console.log(countSegments("Hello, my name is John"));
		</script>
	</body>
</html>
